package com.sxl.domain.model;

import com.sxl.domain.model.entity.ThreadPoolConfigEntity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author : sxl
 * @date : 2024/8/13 8:47
 * @description:
 */

public class DynamicThreadPoolService implements IDynamicThreadPoolService {

    private final String applicationName;
    private final Map<String, ThreadPoolExecutor> threadPoolExecutorMap;;
    public DynamicThreadPoolService(String applicationName, Map<String, ThreadPoolExecutor> threadPoolExecutorMap) {
        this.applicationName = applicationName;
        this.threadPoolExecutorMap = threadPoolExecutorMap;
    }

    /**
     * 查询线程池列表
     * @return
     */
    @Override
    public List<ThreadPoolConfigEntity> queryThreadPoolList() {
        Set<String> threadPoolBeanNames = threadPoolExecutorMap.keySet(); //获取所有线程池的Bean名称
        List<ThreadPoolConfigEntity> threadPoolVOS = new ArrayList<>(threadPoolBeanNames.size());
        for (String threadPoolBeanName : threadPoolBeanNames) {
            ThreadPoolExecutor threadPoolExecutor = threadPoolExecutorMap.get(threadPoolBeanName); //根据bean名称获取线程池
            ThreadPoolConfigEntity threadPoolVO = new ThreadPoolConfigEntity(applicationName,threadPoolBeanName);
            //封装线程池信息
            threadPoolVO.setCorePoolSize(threadPoolExecutor.getCorePoolSize());
            threadPoolVO.setMaximumPoolSize(threadPoolExecutor.getMaximumPoolSize());
            threadPoolVO.setActiveCount(threadPoolExecutor.getActiveCount());
            threadPoolVO.setPoolSize(threadPoolExecutor.getPoolSize());
            threadPoolVO.setQueueType(threadPoolExecutor.getQueue().getClass().getSimpleName());
            threadPoolVO.setQueueSize(threadPoolExecutor.getQueue().size());
            threadPoolVO.setRemainingCapacity(threadPoolExecutor.getQueue().remainingCapacity());
            threadPoolVOS.add(threadPoolVO);
        }
        return threadPoolVOS;
    }

    /**
     * 根据线程池名称查询线程池配置信息
     * @param threadPoolName
     * @return
     */
    @Override
    public ThreadPoolConfigEntity queryThreadPoolConfigByNane(String threadPoolName) {
        if(null==threadPoolExecutorMap)return new ThreadPoolConfigEntity(applicationName, threadPoolName);
        //线程池配置信息
        ThreadPoolExecutor threadPoolExecutor = threadPoolExecutorMap.get(threadPoolName);
        ThreadPoolConfigEntity threadPoolVO = new ThreadPoolConfigEntity(applicationName, threadPoolName);
        threadPoolVO.setCorePoolSize(threadPoolExecutor.getCorePoolSize());
        threadPoolVO.setMaximumPoolSize(threadPoolExecutor.getMaximumPoolSize());
        threadPoolVO.setActiveCount(threadPoolExecutor.getActiveCount());
        threadPoolVO.setPoolSize(threadPoolExecutor.getPoolSize());
        threadPoolVO.setQueueType(threadPoolExecutor.getQueue().getClass().getSimpleName());
        threadPoolVO.setQueueSize(threadPoolExecutor.getQueue().size());
        threadPoolVO.setRemainingCapacity(threadPoolExecutor.getQueue().remainingCapacity());
        return threadPoolVO;
    }

    /**
     * 更新线程池配置信息
     * @param threadPoolConfigEntity
     */
    @Override
    public void updateThreadPoolConfig(ThreadPoolConfigEntity threadPoolConfigEntity) {
        if(null==threadPoolConfigEntity||!applicationName.equals(threadPoolConfigEntity.getAppName())) return;
        ThreadPoolExecutor threadPoolExecutor = threadPoolExecutorMap.get(threadPoolConfigEntity.getThreadPoolName());
        if(null == threadPoolExecutor) return;
        //设置参数，调整核心线程数和最大线程数
        threadPoolExecutor.setCorePoolSize(threadPoolConfigEntity.getCorePoolSize());
        threadPoolExecutor.setMaximumPoolSize(threadPoolConfigEntity.getMaximumPoolSize());
    }
}
